package Code.Objects.Characters.Enemies.Basic;

import org.jrabbit.standard.game.graphics.skins.animation.AnimatedSkin;

import Code.Basic.Entity.GenericCharacter;

public abstract class MobileCharacter extends GenericCharacter
{
	protected float moveSpeed;
	protected double turnSpeed;
	
	public MobileCharacter(String filepath)
	{
		super(filepath);
	}

	public MobileCharacter(AnimatedSkin skin)
	{
		super(skin);
	}
	
	public void rotateAndMoveTowards(double angle)
	{
		switch(getRotationDirection(angle))
		{
			case -1: turnLeft(); break;
			case 0: faceAngle(angle); break;
			case 1: turnRight(); break;
		}
		moveForward(moveSpeed);
	}
	
	public void rotateTowards(double angle)
	{
		switch(getRotationDirection(angle))
		{
			case -1: turnLeft(); break;
			case 0: faceAngle(angle); break;
			case 1: turnRight(); break;
		}
	}
	
	protected void moveTowards(double angle)
	{
		faceAngle(angle);
		moveForward(moveSpeed);
	}
	
	protected void faceAngle(double angle)
	{
		setRotation(angle);
	}
	
	protected void turnLeft()
	{
		rotate(-turnSpeed);
	}
	
	protected void turnRight()
	{
		rotate(turnSpeed);
	}
	
	public int getRotationDirection(double angle)
	{
		double offset = angle - rotation.angle();
		
		if(offset < -180)
		{
			offset += 360;
		}
		else if(offset > 180)
		{
			offset -= 360;
		}
		
		if(offset > turnSpeed)
		{
			return 1;
		}
		else if(offset < -turnSpeed)
		{
			return -1;
		}
		return 0;
	}
}
